//
//  MutiScrollViewController.m
//  Tools
//
//  Created by han on 2019/1/9.
//  Copyright © 2019 han. All rights reserved.
//

#import "MutiScrollViewController.h"
#import "HeaderViewController.h"
#import "HeadSegue.h"


@interface MutiScrollViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) UIView *headview;
@property (weak, nonatomic) IBOutlet UIView *coverNaviView;
@property (weak, nonatomic) IBOutlet UIView *mainNaviView;
@property (nonatomic, strong) HeaderViewController *childVC;
@property (weak, nonatomic) IBOutlet UIView *contentheader;

@end

@implementation MutiScrollViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self performSegueWithIdentifier:@"headIdentifier" sender:nil];
    [self setupSubviews];
}
- (void)setupSubviews {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.tableView.mj_header endRefreshing];
        });
    }];
    self.tableView.mj_header.ignoredScrollViewContentInsetTop = -400;
    self.tableView.scrollIndicatorInsets = UIEdgeInsetsMake(400, 0, 0, 0);
    // ib 中拖出来view
    self.contentheader.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 400);
}
- (IBAction)buttonAction:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
    
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.headview.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 400);
}
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"headIdentifier"]) {
        HeaderViewController *childVC = [segue destinationViewController];
        UIView *headview = [segue destinationViewController].view;
        headview.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 400);
        [self addChildViewController:childVC];
//        self.tableView.tableHeaderView = headview;
        [self.tableView addSubview:headview];
    
//        [self.tableView setContentInset:UIEdgeInsetsMake(400, 0, 0, 0)];
        self.childVC = childVC;
        self.headview = headview;
    }
}

#pragma mark -- tableviewdelegate & datasource
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 20;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 15;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"tableviewCellIdentifier" forIndexPath:indexPath];
    cell.textLabel.text = @"444";
    return cell;
}
#pragma mark -- scrollviewdelegate
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    CGFloat contentoffsetY = scrollView.contentOffset.y;
    if (contentoffsetY > 0 && contentoffsetY < 24) {
        [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    } else if (contentoffsetY >= 24 && contentoffsetY < 95) {
        [self.tableView setContentOffset:CGPointMake(0, 95) animated:YES];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
   CGFloat contentoffsetY = scrollView.contentOffset.y;
    if (contentoffsetY <= 0) {
        self.headview.frame = CGRectMake(0, contentoffsetY, [UIScreen mainScreen].bounds.size.width, 400);
        [self.childVC changeAlpha:1];
        self.coverNaviView.alpha = 0;
        self.mainNaviView.alpha = 1;
    } else if (contentoffsetY > 0 && contentoffsetY < 95) {
        CGFloat alpha = (1- contentoffsetY /95) > 0 ? (1-contentoffsetY/95): 0;
        [self.childVC changeAlpha:alpha/3];
        if (alpha > 0.9) {
            self.coverNaviView.alpha = 0;
            self.mainNaviView.alpha = alpha/5;
        }else {
            self.coverNaviView.alpha = 1 - alpha;
            self.mainNaviView.alpha = 0;
            if (alpha <=0.75) {
                [self.childVC changeAlpha:0];
            }
        }
    }
}

@end
